﻿using AutoMapper;
using MediatR;
using Traceable.App.Application.Command.Aquaculture;
using Traceable.App.DTOS;
using Traceable.Domian;
using Traceable.ErrorCode;
using Traceable.Infastructure;

namespace Traceable.App.Application.Handler.Aquaculture
{
    public class GetImageListHandler : IRequestHandler<GetImageListCommand, ApiResult<List<ImageDto>>>
    {
        private readonly IBaseRepository<Image_Table> _imageRepository;
        private readonly IMapper _mapper;

        public GetImageListHandler(IBaseRepository<Image_Table> imageRepository, IMapper mapper)
        {
            _imageRepository = imageRepository;
            _mapper = mapper;
        }
        /// <summary>
        /// 获取图片信息
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<ApiResult<List<ImageDto>>> Handle(GetImageListCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<ImageDto>> result = new ApiResult<List<ImageDto>>();

            var image = _imageRepository.GetAll().Where(p => p.Batch_Id == request.Batch_Id && p.Traceable_Id == request.Traceable_Id
                                                       && p.ImgType == request.ImgType);
            if(image != null)
            {
                result.Code = StatusEnum.OK;
                result.Data=_mapper.Map<List<ImageDto>>(image);
                result.Message = "获取成功";
            }
            return Task.FromResult(result);
        }
    }
}
